package cn.lili.modules.wallet.serviceimpl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.order.trade.entity.vo.WalletLogQueryVO;
import cn.lili.modules.payment.entity.dos.WalletLog;
import cn.lili.modules.payment.entity.enums.WalletServiceTypeEnum;
import cn.lili.modules.wallet.mapper.WalletLogMapper;
import cn.lili.modules.wallet.service.WalletLogService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 预存款日志业务层实现
 *
 * @author pikachu
 * @since 2020-02-25 14:10:16
 */
@Service
public class WalletLogServiceImpl extends ServiceImpl<WalletLogMapper, WalletLog> implements WalletLogService {

    @Override
    public Page<WalletLog> depositLogPage(PageVO page, WalletLogQueryVO walletLogQueryVO) {
        //构建查询条件
        QueryWrapper<WalletLog> walletLogQueryWrapper = new QueryWrapper<>();
        //会员名称
        walletLogQueryWrapper.like(!CharSequenceUtil.isEmpty(walletLogQueryVO.getNickName()), "nick_name",
                walletLogQueryVO.getNickName());
        //会员id
        walletLogQueryWrapper.eq(!CharSequenceUtil.isEmpty(walletLogQueryVO.getUserId()), "user_id",
                walletLogQueryVO.getUserId());
        //开始时间和技术时间
        if (!StringUtils.isEmpty(walletLogQueryVO.getStartDate()) && !StringUtils.isEmpty(walletLogQueryVO.getEndDate())) {
            Date start = cn.hutool.core.date.DateUtil.parse(walletLogQueryVO.getStartDate());
            Date end = cn.hutool.core.date.DateUtil.parse(walletLogQueryVO.getEndDate());
            walletLogQueryWrapper.between("create_time", start, end);
        }
        if (null != walletLogQueryVO.getType()) {
            if (walletLogQueryVO.getType() == 1) {
                walletLogQueryWrapper.gt("money", 0);
            }else {
                walletLogQueryWrapper.lt("money", 0);
            }
        }
        walletLogQueryWrapper.orderByDesc("create_time");
        //查询返回数据
        Page<WalletLog> result = this.page(PageUtil.initPage(page), walletLogQueryWrapper);
        if (!result.getRecords().isEmpty()) {
            result.getRecords().forEach(walletLog -> {
                if (walletLog.getMoney() > 0) {
                    walletLog.setType(1);
                }else {
                    walletLog.setType(0);
                }

                if (WalletServiceTypeEnum.PAY.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("订单支付");
                }else if (WalletServiceTypeEnum.GIVE.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("会议返现");
                }else if (WalletServiceTypeEnum.WITHDRAWAL.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("余额提现");
                }else if (WalletServiceTypeEnum.RECHARGE.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("余额充值");
                }else if (WalletServiceTypeEnum.REFUND.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("退款");
                }else if (WalletServiceTypeEnum.COMMISSION.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("佣金");
                }else if (WalletServiceTypeEnum.SETTLEMENT.name().equals(walletLog.getServiceType())) {
                    walletLog.setServiceTypeStr("平台结算");
                }
            });
        }
        return result;
    }

    @Override
    public WalletLog getWalletLogByOutTradeNo(String outTradeNo) {
        QueryWrapper<WalletLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no", outTradeNo);
        return this.getOne(queryWrapper);
    }
}